*  Project title:     'Sovereign Risk and Government Change: Elections, Ideology and Experience'
*  Authors:           Sarah M. Brooks; Raphael Cunha; Layna Mosley
*  File description:  GARCH analysis of monthly EMBI and CDS spreads
*  Output:            Tables in Section G of Supplementary Appendix


* change ADO folder if you don't have permissions
*sysdir set PLUS "...\new_ado"

* install estout package for latex tables
ssc install estout, replace
ssc install dataout, replace
ssc install parmest, replace

* set working directory
cd "...\Data\Monthly\"

* load data
use "...\Data\Monthly\Spread_dat_monthly.dta", clear


* pooled panel GARCH(1,1) with AR(1) for conditional mean

arch d_embispread monthsoffc execleft monthsoffcxexecleft election_window ///
cab_annual_gdp extdebt_gni_annual shortdebt_reserves_annual growth_annual d_inflation ///
d_treasury10y d_vix d_energy_index d_eqprem d_embispreadregiondiff kaopen globaldefault, ///
het(monthsoffc execleft monthsoffcxexecleft election_window d_vix kaopen) ///
ar(1) arch(1) garch(1)

eststo garch1

arch d_cdsspread monthsoffc execleft monthsoffcxexecleft election_window ///
cab_annual_gdp extdebt_gni_annual shortdebt_reserves_annual growth_annual d_inflation ///
d_treasury10y d_vix d_energy_index d_eqprem d_embispreadregiondiff kaopen globaldefault, ///
het(monthsoffc execleft monthsoffcxexecleft election_window d_vix kaopen) ///
ar(1) arch(1) garch(1)

eststo garch2

* pooled panel GARCH-in-Mean(1,1) with AR(1) for conditional mean

arch d_embispread monthsoffc execleft monthsoffcxexecleft election_window ///
cab_annual_gdp extdebt_gni_annual shortdebt_reserves_annual growth_annual d_inflation ///
d_treasury10y d_vix d_energy_index d_eqprem d_embispreadregiondiff kaopen globaldefault, ///
het(monthsoffc execleft monthsoffcxexecleft election_window d_vix kaopen) ///
archm ar(1) arch(1) garch(1)

eststo garch3

arch d_cdsspread monthsoffc execleft monthsoffcxexecleft election_window ///
cab_annual_gdp extdebt_gni_annual shortdebt_reserves_annual growth_annual d_inflation ///
d_treasury10y d_vix d_energy_index d_eqprem d_embispreadregiondiff kaopen globaldefault, ///
het(monthsoffc execleft monthsoffcxexecleft election_window d_vix kaopen) ///
archm ar(1) arch(1) garch(1)

eststo garch4

* results table
esttab garch1 garch3 garch2 garch4, b se star(* 0.10 ** 0.05 *** 0.01) aic bic scalars(ll)

* save to file
esttab garch1 garch3 garch2 garch4 using garch_table.tex, ///
b(3) se star(* 0.10 ** 0.05 *** 0.01) nogaps aic bic scalars(ll) longtable tex replace


* country-specific GARCH(1,1) models (EMBI only; not enough CDS data)

capture postutil clear
postfile handle str20 clabel b_het_monthsoffcxexecleft ///
                      se_het_monthsoffcxexecleft ///
					  sample_size using country_garch_embi, replace

levelsof country, local(allCn)

foreach cn of local allCn {

	capture noisily arch d_embispread monthsoffc execleft monthsoffcxexecleft if country == `"`cn'"', ///
	het(monthsoffc execleft monthsoffcxexecleft) arch(1) garch(1)

	if _rc == 0 {

		capture display _b[HET:monthsoffcxexecleft]

		if _rc != 111 {

			post handle ("`cn'") (_b[HET:monthsoffcxexecleft]) ///
			                     (_se[HET:monthsoffcxexecleft]) ///
			                     (e(N))
		}
	}
}
postclose handle


* export coefs to latex table

* load data
use "...\Data\Monthly\country_garch_embi.dta", clear

gen z_het_monthsoffcxexecleft = b_het_monthsoffcxexecleft/se_het_monthsoffcxexecleft
gen p_het_monthsoffcxexecleft = 2*normal(-abs(z_het_monthsoffcxexecleft))

format b_het_monthsoffcxexecleft se_het_monthsoffcxexecleft z_het_monthsoffcxexecleft p_het_monthsoffcxexecleft %9.3f
format sample_size %9.0f

order clabel b_het_monthsoffcxexecleft se_het_monthsoffcxexecleft z_het_monthsoffcxexecleft p_het_monthsoffcxexecleft sample_size

dataout, save(country_garch_embi) dec(3) tex replace
